import time
import modbus_stack

if __name__== '__main__':
    mb = modbus_stack.Master('COM3', 115200)
    mb.master_stack_start()
    i = 300
    while True:
        '''
        ret_coil = mb.modbus_read_coil(0, 100)
        if ret_coil:
            print("modbus_read_coil - >")
            for k , v in enumerate(ret_coil):
                print("%02X " %v, end="")
            print("")
        time.sleep(0.1)
        ret_coil = mb.modbus_read_input_status(0, 100)
        if ret_coil:
            print("modbus_read_input_status - >")
            for k , v in enumerate(ret_coil):
                print("%02X " %v, end="")
            print("")
        time.sleep(0.1)
        ret_coil = mb.modbus_read_hold_reg(0, 100)
        if ret_coil:
            print("modbus_read_hold_reg - >")
            for k , v in enumerate(ret_coil):
                print("%04X " %v, end="")
            print("")
        time.sleep(0.1)
        ret_coil = mb.modbus_read_input_reg(0, 100)
        if ret_coil:
            print("modbus_read_input_reg - >")
            for k , v in enumerate(ret_coil):
                print("%04X " %v, end="")
            print("")
        time.sleep(0.1)
        '''
        if i % 2 == 0:
            mb.modbus_write_single_coil(i, 0xFF00)
        else:
            mb.modbus_write_single_coil(i, 0)
        time.sleep(0.05)
        mb.modbus_write_single_reg(i, i)
        time.sleep(0.05)
        i = i + 1